<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------
namespace app\services\care;

use app\model\care\ServiceOrder;
use app\model\care\ServiceOrderComment;
use app\services\BaseServices;
use app\dao\care\ServiceOrderCommentDao;
use crmeb\exceptions\AdminException;
use crmeb\traits\ServicesTrait;
use crmeb\services\FormBuilder as Form;
use think\facade\Route as Url;

class ServiceOrderCommentServices extends BaseServices
{

    public function __construct(ServiceOrderCommentDao $dao){
        $this->dao = $dao;
    }

    public function getList(array $where = [],$field = '*', bool $is_page = true)
    {
        $where['is_del'] = 0;
        $page = $limit = 0;
        if($is_page) {
            [$page,$limit] = $this->getPageValue();
        }
        $list = $this->dao->getList($where,$field,$page,$limit);
        foreach ($list['list'] as &$item) {
            $item['images'] = explode(',', $item['images']);
        }
        return $list;
    }

    public function add(array $data = []) {
        if($this->save($data)){
            return '评价成功';
        }
        throw new AdminException('评价失败');

    }

    public function del(int $id) {
        if($this->dao->get($id)) {
            return $this->dao->del((int)$id);
        }
        throw new AdminException('删除失败');
    }

    /**
     * 用户评论
     * @param $data
     * @param $user
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function comment($data,$user){
        $insert = [
            'uid'=>$user->uid,
            'order_id'=>$data['order_id'],
            'content'=>$data['content'],
            'service_score'=>$data['service_score'],
            'add_time'=>time(),
            'images'=>'',
        ];
        $this->transaction(function () use ($insert,$data) {
            $res = $this->dao->save($insert);
            $order = ServiceOrder::where('id',$data['order_id'])->find();
            $ores = $order->save(['is_comment'=>2]);
            if(!$res) throw new AdminException('评论失败');
        });
        return '评论成功';
    }

    /**
     * 评论审核Form
     * @param int $comment_id
     * @return array
     * @throws \FormBuilder\Exception\FormBuilderException
     */
    public function orderCommentVerify(int $comment_id){
        $comment = $this->dao->getCommentInfo($comment_id)[0];
        $field = array();
        $field[] = Form::hidden('id',$comment_id);
        $field[] = Form::input('nickname', '用户昵称', $comment['nickname'] ?? '')->disabled(true);
        $field[] = Form::input('content', '评论内容', $comment['content'] ?? '')->type('textarea')->disabled(true);
//        $field[] = Form::input('reply', '评论回复', $comment['reply'] ?? '')->type('textarea');
        $field[] = Form::radio('status', '审核状态', 10)->options([['label' => '已通过', 'value' => 10],['label' => '未通过', 'value' => 20]]);
        $title = '订单评论审核';
        return create_form($title, $field, Url::buildUrl('/care/service/order_comment_verify/save'),'POST');
    }

    /**
     * 评论审核保存
     * @param int $id
     * @param array $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(int $id,array $data){
        $comment = ServiceOrderComment::where('id',$id)->find();
        if($data['status'] == 10){// 审核通过
            $res = $comment->save(['status'=>10,'reply'=>$data['reply']]);
        } else { // 审核未通过
            $res = $comment->save(['status'=>20,'reply'=>$data['reply']]);
        }
        if(!$res) throw new AdminException('审核失败');
        return true;
    }
}
